//
//  ECUThirdHeadCell.m
//  Edu_iOS
//
//  Created by apple on 2017/9/23.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#define TitleLab_Top_Margin 15
#define TitleLab_TimeLab_Margin 7


#import "ECUThirdHeadCell.h"

#import "ECUCatalogModel.h"

#import "ContactDateHelper.h"

@interface ECUThirdHeadCell()

@property (nonatomic, strong) UIImageView *circleImageView;
@property (nonatomic, strong) UIView *verticalLineView;//竖线
@property (nonatomic, strong) UIView *horizontalLineView;//横线
@property (nonatomic, strong) UIImageView *dataTypeImageView;//视频或者其他类型的小图片
@property (nonatomic, strong) UIButton *isFreeImageView;//是否需免费试听

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *learnStatusLab;//学习状态lab

@end


@implementation ECUThirdHeadCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initCellUI];
    }
    return self;
}

- (void)initCellUI{
    self.selectionStyle = 0;
    
    WeakSelf(weakSelf);
    
    _horizontalLineView = [[UIView alloc]init];
    [self.contentView addSubview:_horizontalLineView];
    _horizontalLineView.backgroundColor = [UIColor colorWithHexString:@"#DEDFE7"];
    [_horizontalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(weakSelf.contentView.mas_height);
        make.left.mas_equalTo(weakSelf.contentView.mas_left).offset(48.f*KProportion1080_Width + 4.5);
        make.width.mas_equalTo(1);
    }];
    
    _circleImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:_circleImageView];
    _circleImageView.image = [UIImage imageNamed:@"catalog_circleShadow"];
    [_circleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.contentView.mas_left).offset(48*KProportion1080_Width);
        make.centerY.mas_equalTo(weakSelf.contentView.mas_centerY);
        make.width.height.mas_equalTo(10);
    }];
    
    _dataTypeImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:_dataTypeImageView];
    [_dataTypeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.contentView.mas_top).offset(TitleLab_Top_Margin);
        make.left.mas_equalTo(weakSelf.contentView.mas_left).offset(kProportion_Width(96) + 12.5);
        make.width.mas_equalTo(38.f*KProportion1080_Width);
        make.height.mas_equalTo(weakSelf.dataTypeImageView.mas_width).multipliedBy(28.f/38.f);
    }];
    
    _titleLab = [[UILabel alloc]init];
    [self.contentView addSubview:_titleLab];
    _titleLab.textColor = [UIColor colorWithHexString:@"#293039"];
    _titleLab.textAlignment = NSTextAlignmentLeft;
    _titleLab.font = [UIFont systemFontOfSize:15];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.dataTypeImageView.mas_right).offset(28.f*KProportion1080_Width);
        make.centerY.mas_equalTo(weakSelf.dataTypeImageView.mas_centerY);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).offset(-10);
        make.height.mas_equalTo(15);
    }];
    
    _timeLab = [[UILabel alloc]init];
    [self.contentView addSubview:_timeLab];
    _timeLab.textColor = [UIColor colorWithHexString:@"#73757b"];
    _timeLab.textAlignment = NSTextAlignmentLeft;
    _timeLab.font = [UIFont systemFontOfSize:12];
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.titleLab.mas_left);
        make.top.mas_equalTo(weakSelf.titleLab.mas_bottom).offset(TitleLab_TimeLab_Margin);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(14);
    }];
    
    //根据标识判断是否显示
    _isFreeImageView = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_isFreeImageView];
    [_isFreeImageView setTitle:@"免费试学" forState:0];
    [_isFreeImageView setTitleColor:[UIColor colorWithHexString:@"#3FBB22"] forState:0];
    _isFreeImageView.titleLabel.font = [UIFont systemFontOfSize:10.f];
    _isFreeImageView.layer.masksToBounds = YES;
    _isFreeImageView.layer.cornerRadius = 2.f;
    _isFreeImageView.layer.borderWidth = 1.f;
    _isFreeImageView.layer.borderColor = [UIColor colorWithHexString:@"#3fbb22"].CGColor;
    [_isFreeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.timeLab.mas_centerY);
        make.left.mas_equalTo(weakSelf.timeLab.mas_right).offset(5);
        make.width.mas_equalTo(150.f*KProportion1080_Width);
        make.height.mas_equalTo(weakSelf.isFreeImageView.mas_width).multipliedBy(45.f/150.f);
    }];
    
    _learnStatusLab = [[UILabel alloc]init];
    [self.contentView addSubview:_learnStatusLab];
    _learnStatusLab.textAlignment = NSTextAlignmentRight;
    _learnStatusLab.textColor = [UIColor colorWithHexString:@"#73757b"];
    _learnStatusLab.font = [UIFont systemFontOfSize:36*KProportion1080_Heigh];
    [_learnStatusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.contentView.mas_right).offset(-10);
        make.centerY.mas_equalTo(weakSelf.timeLab.mas_centerY);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(20);
    }];
    
    _verticalLineView = [[UIView alloc]init];
    [self.contentView addSubview:_verticalLineView];
    _verticalLineView.backgroundColor = [UIColor colorWithHexString:@"#dedfe7"];
    [_verticalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.titleLab.mas_left);
        make.right.mas_equalTo(weakSelf.contentView);
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom);
        make.height.mas_equalTo(1);
    }];
    
}

//-(void)setDateOfCellWithModel:(ECUCatalogModel *)model{
//    _titleLab.text = model.text;
//    _learnStatusLab.text = @"待学习";
//    _dataTypeImageView.image = [UIImage imageNamed:@"catalog_video_normal"];
//}
//
//-(void)didSelectOfCellWithLearnStatus:(ECUCatalogModel *)model{
//    _learnStatusLab.text = @"学习中";
//    _learnStatusLab.textColor = [UIColor colorWithHexString:@"#FF8E45"];
//}




//*************************************************************************************************

-(void)setDateOfCellWithModel:(ECListModel *)model{
    self.titleLab.text = model.title;
    self.learnStatusLab.textColor = [UIColor colorWithHexString:@"#73757b"];

    if ([model.data.learnStatus isEqualToString:@"learning"]) {

    }else if([model.data.learnStatus isEqualToString:@"finished"]){
        self.learnStatusLab.text = @"已学完";
    }else{
        self.learnStatusLab.text = @"";
    }
    
    if ([model.isLearning isEqualToString:@"正在学"]) {
        self.learnStatusLab.text = @"正在学";
        self.learnStatusLab.textColor = [UIColor colorWithHexString:@"#FF8E45"];
    }else if ([model.isLearning isEqualToString:@"已学完"]){
        self.learnStatusLab.text = @"已学完";
        self.learnStatusLab.textColor = [UIColor colorWithHexString:@"#73757b"];
    }

    self.learnStatusLab.hidden = NO;
    if ([model.type isEqualToString:@"text"]) {//图文类型
        self.timeLab.text = @"文本资料";
    }else if([model.type isEqualToString:@"testpaper"]){//试卷类型
        self.timeLab.text = @"试卷";
        self.learnStatusLab.hidden = YES;
    }else{//音视频类型
        self.timeLab.text = [NSString stringWithFormat:@"%@",[[ContactDateHelper sharedInstance] getMMSSFromSS:model.data.length]];
    }
    
    if ([model.data.free isEqualToString:@"1"]) {//免费试听课时
        self.isFreeImageView.hidden = NO;
    }else{
        self.isFreeImageView.hidden = YES;
    }
    
    if ([model.data.status isEqualToString:@"unpublished"]) {//课程暂未发布
        [self.isFreeImageView setTitle:@"暂未发布" forState:0];
        self.isFreeImageView.hidden = NO;
    }
    
    //图标
    if ([model.type isEqualToString:@"audio"]) {//音频
        self.dataTypeImageView.image = [UIImage imageNamed:@"catalog_audio"];
    }else if ([model.type isEqualToString:@"video"]){//视频
        self.dataTypeImageView.image = [UIImage imageNamed:@"catalog_video"];
    }else if ([model.type isEqualToString:@"text"]){//文本
        self.dataTypeImageView.image = [UIImage imageNamed:@"catalog_word"];
    }else{//试卷
        self.dataTypeImageView.image = [UIImage imageNamed:@"catalog_testpaper"];
    }
    
    if (model.level == 0) {
        
        [self.dataTypeImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(48.f*KProportion1080_Width + 5.5);
            make.top.mas_equalTo(self.contentView.mas_top).offset(TitleLab_Top_Margin);
            make.width.mas_equalTo(38.f*KProportion1080_Width);
            make.height.mas_equalTo(self.dataTypeImageView.mas_width).multipliedBy(28.f/38.f);
        }];
        
        self.horizontalLineView.hidden = YES;
        self.circleImageView.hidden = YES;
        
    }else if (model.level == 1){
        
        [self.dataTypeImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(118.f*KProportion1080_Width + 5.5);
            make.top.mas_equalTo(self.contentView.mas_top).offset(TitleLab_Top_Margin);
            make.width.mas_equalTo(38.f*KProportion1080_Width);
            make.height.mas_equalTo(self.dataTypeImageView.mas_width).multipliedBy(28.f/38.f);
        }];
        
        self.horizontalLineView.hidden = YES;
        self.circleImageView.hidden = YES;
        
    }else if (model.level == 2){
        
        [self.dataTypeImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(118.f*KProportion1080_Width + 5.5);
            make.top.mas_equalTo(self.contentView.mas_top).offset(TitleLab_Top_Margin);
            make.width.mas_equalTo(38.f*KProportion1080_Width);
            make.height.mas_equalTo(self.dataTypeImageView.mas_width).multipliedBy(28.f/38.f);
        }];
        
        self.horizontalLineView.hidden = NO;
        self.circleImageView.hidden = YES;
    }
}

-(void)didSelectOfCellWithLearnStatus:(ECListModel *)model{
    self.learnStatusLab.text = model.isLearning;
    if ([model.isLearning isEqualToString:@"正在学"]) {
        self.learnStatusLab.textColor = [UIColor colorWithHexString:@"#FF8E45"];
    }else{
        self.learnStatusLab.textColor = [UIColor colorWithHexString:@"#73757b"];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
